package sdc.media.motorbike.View;

import sdc.media.motorbike.MotorBike;
import sdc.media.motorbike.Controller.Assets;
import sdc.media.motorbike.Controller.Constant;
import sdc.media.motorbike.Controller.MyInputProcessor;
import sdc.media.motorbike.Controller.Setting;
import sdc.media.motorbike.Controller.WorldGame;
import sdc.media.motorbike.Model.Car;
import sdc.media.motorbike.Model.Motor;
import sdc.media.motorbike.Model.Object;
import sdc.media.motorbike.Model.UserData;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class PlayScreen extends BaseScreen {

	// Game state
	public enum STATE {
		GAME_READY, GAME_RUNNING, GAME_PAUSE, GAME_LEVEL_END, GAME_OVER
	}

	private STATE state;
	
	// Button, etc...
	private int lastScore;
	private boolean isSpeed;
	private boolean isQuit;
	private boolean isPauseClick;
	private boolean isResumeClick;
	private boolean isQuitClick;
	
	// World
	public WorldGame worldGame;
	public OrthographicCamera camera2;
	
	// Tilt
	boolean available;
	Orientation orientation;
	
	@Override
	public void show() {
		super.show();
		
		// Play Music and Sound
		if (Setting.soundEnabled) {
			// Play music and sound
			Assets.backgroundPlayMusic.pause();
			Assets.dichuyen.play();
		} else {
			//Pause music and sound
			Assets.backgroundPlayMusic.pause();
			Assets.dichuyen.pause();
		}
		
		// Constructor for button, etc...
		
		// Event
		
		// State
		state = STATE.GAME_READY;
		
		// World
		worldGame = new WorldGame();
		camera2 = new OrthographicCamera(Gdx.graphics.getWidth() / Constant.PIXEL_PER_METER, Gdx.graphics.getHeight() / Constant.PIXEL_PER_METER);
		lastScore = worldGame.score;
		
		// tilt
		available = Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer);
		orientation = Gdx.input.getNativeOrientation();
		
		Gdx.input.setInputProcessor(new MyInputProcessor(){
			@Override
			public boolean keyDown(int keycode) {
				// TODO Auto-generated method stub
				switch (keycode)
				{
				case Keys.A:
					// Move left
					worldGame.motorbike.moveState = Motor.MoveState.MOVE_LEFT;
					break;
				case Keys.S:
					// Stop
					worldGame.motorbike.moveState = Motor.MoveState.IDLE;
					break;
				case Keys.D:
					// Move right
					worldGame.motorbike.moveState = Motor.MoveState.MOVE_RIGHT;
					break;
				case Keys.W:
					// Move up
					//worldGame.motorbike.moveState = Motor.MoveState.MOVE_UP;
					break;
				case Keys.J:
					// Jump
					
					break;
				case Keys.P:
					if(state == STATE.GAME_RUNNING)
						state = STATE.GAME_PAUSE;
					else
						state = STATE.GAME_RUNNING;
				
				default:
					
					break;
				}
				return true;
			}
			
			@Override
			public boolean touchDown(int screenX, int screenY, int pointer,
					int button) {
				// TODO Auto-generated method stub
				if(screenX >= 0 && screenX <= 200 * worldGame.sizeX && screenY >= (Gdx.graphics.getHeight() - 100 * worldGame.sizeY) && screenY <= Gdx.graphics.getHeight()) {
					if(!isQuit) {
						if(state == STATE.GAME_RUNNING || state == STATE.GAME_PAUSE) {
							if(state == STATE.GAME_RUNNING) {
								//state = STATE.GAME_PAUSE;
								isPauseClick = true;
							}
							else {
								//state = STATE.GAME_RUNNING;
								isResumeClick = true;
							}
						}
					}
				}
				else if(screenX >= (Gdx.graphics.getWidth() - 200 * worldGame.sizeX) && screenX <= Gdx.graphics.getWidth() && screenY >= (Gdx.graphics.getHeight() - 100 * worldGame.sizeY) && screenY <= Gdx.graphics.getHeight()) {
					if(!isQuit) {
						if(state == STATE.GAME_PAUSE) {
							//state = STATE.GAME_PAUSE;
							//isQuit = true;
							isQuitClick = true;
						}
					}
				}
				else {
					if(state == STATE.GAME_PAUSE) {
						if(screenX >=  50 * worldGame.sizeX && screenX <= 220 * worldGame.sizeX && screenY >= 400 * worldGame.sizeY && screenY <= 500 * worldGame.sizeY) {
							isResumeClick = true;
						}
						else if(screenX >=  280 * worldGame.sizeX && screenX <= 440 * worldGame.sizeX && screenY >= 400 * worldGame.sizeY && screenY <= 500 * worldGame.sizeY) {
							isQuitClick = true;
						}
					} else {
						if(state == STATE.GAME_RUNNING) {
							if(worldGame.motorbike.startTime == 0)
								worldGame.motorbike.startTime = System.nanoTime();
							Assets.dichuyen.pause();
							Assets.tangtoc.play();
							isSpeed = true;
						}
					}
				}
				return super.touchDown(screenX, screenY, pointer, button);
			}
			
			@Override
			public boolean touchUp(int screenX, int screenY, int pointer,
					int button) {
				// TODO Auto-generated method stub
				if(screenX >= 0 && screenX <= 200 * worldGame.sizeX && screenY >= (Gdx.graphics.getHeight() - 150 * worldGame.sizeY) && screenY <= Gdx.graphics.getHeight()) {
					if(!isQuit) {
						if(state == STATE.GAME_RUNNING || state == STATE.GAME_PAUSE) {
							if(state == STATE.GAME_RUNNING) {
								state = STATE.GAME_PAUSE;
								isPauseClick = false;
							}
							else {
								state = STATE.GAME_RUNNING;
								isResumeClick = false;
							}
						}
					}
				}
				else if(screenX >= (Gdx.graphics.getWidth() - 200 * worldGame.sizeX) && screenX <= Gdx.graphics.getWidth() && screenY >= (Gdx.graphics.getHeight() - 100 * worldGame.sizeY) && screenY <= Gdx.graphics.getHeight()) {
					if(!isQuit) {
						if(state == STATE.GAME_PAUSE) {
							//state = STATE.GAME_PAUSE;
							isQuit = true;
							isQuitClick = false;
						}
					}
				} else {
					if(state == STATE.GAME_PAUSE) {
						if(screenX >=  50 * worldGame.sizeX && screenX <= 220 * worldGame.sizeX && screenY >= 400 * worldGame.sizeY && screenY <= 500 * worldGame.sizeY) {
							if(isQuit) {
								state = STATE.GAME_OVER;
								isQuit = false;
								isQuitClick = false;
							}
						}
						else if(screenX >=  280 * worldGame.sizeX && screenX <= 440 * worldGame.sizeX && screenY >= 400 * worldGame.sizeY && screenY <= 500 * worldGame.sizeY) {
							if(isQuit) {
								state = STATE.GAME_RUNNING;
								isQuit = false;
								isQuitClick = false;
							}
						}
					} else {
						if(state == STATE.GAME_RUNNING) {
							Assets.dichuyen.play();
							Assets.tangtoc.pause();
							worldGame.motorbike.restartTime();
							isSpeed = false;
						}
					}
				}
				return super.touchUp(screenX, screenY, pointer, button);
			}
			
			@Override
			public boolean scrolled(int amount) {
				// TODO Auto-generated method stub
				camera.zoom += amount / 25f;
				camera.update();
				camera2.zoom += amount / 25f;
				camera2.update();
				return true;
			}
		});
	}
	
	@Override
	public void render(float delta) {
		super.render(delta);
		update(delta);
		draw(delta);
		
		worldGame.debugRenderer.render(worldGame.worldBox2D, camera2.combined);
		worldGame.debugRenderer.setDrawInactiveBodies(false);
		worldGame.debugRenderer.setDrawBodies(false);
		worldGame.worldBox2D.step(Constant.WORLD_STEP, Constant.WORLD_VELOCITY_ITERATION, Constant.WORLD_POSITION_ITERATION);
		drawSpriteToBody();
		scaleObject();
	}

	private void scaleObject() {
		// TODO Auto-generated method stub
		
		// Car
		float scale;
		for (Car car : worldGame.activeCars) {
			//scale = (-car.getSprite().getY() + Gdx.graphics.getHeight() / 2.0f / Constant.PIXEL_PER_METER) * Constant.PIXEL_PER_METER / Gdx.graphics.getHeight();
			scale = Math.abs(worldGame.HEIGHT / 2 - car.getBody().getPosition().y) / worldGame.HEIGHT + 0.5f;
			if(scale >= 1)
				scale = 1;
			car.getSprite().setScale(scale, scale);
		}
		
		// Motorbike
		scale = (-worldGame.motorbike.getSprite().getY() + Gdx.graphics.getHeight() / 2.0f / Constant.PIXEL_PER_METER) * Constant.PIXEL_PER_METER / Gdx.graphics.getHeight();
		if(scale >= 1)
			scale = 1;
		worldGame.motorbike.getSprite().setScale(scale, scale);
		
	}

	private void update(float deltaTime) {
		if (deltaTime > 0.1f)
			deltaTime = 0.1f;
		
		switch (state) {
		case GAME_READY:
			updateReady(deltaTime);
			break;

		case GAME_RUNNING:
			updateRunning(deltaTime);
			break;

		case GAME_PAUSE:
			updatePause(deltaTime);
			break;

		case GAME_LEVEL_END:
			updateLevelEnd(deltaTime);
			break;

		case GAME_OVER:
			updateGameOver(deltaTime);
			break;

		default:
			break;
		}
	}

	// Game Ready
	private void updateReady(float deltaTime) {
		// TODO Auto-generated method stub
		if(Gdx.input.justTouched())
			state = STATE.GAME_RUNNING;
	}

	// Game Running
	private void updateRunning(float deltaTime) {
		// TODO Auto-generated method stub
		System.gc();
		ApplicationType apptype = Gdx.app.getType();
		if (apptype == ApplicationType.Android
				|| apptype == ApplicationType.Desktop)
			worldGame.update(deltaTime);

		// Update Money, etc...
		worldGame.timePlay += deltaTime;
		
		if(isSpeed) {
			worldGame.motorbike.endTime = System.nanoTime();
			worldGame.timeSpeed = (float) ((worldGame.motorbike.endTime - worldGame.motorbike.startTime)/1.0E09);
			worldGame.speed = (float) ((worldGame.timeSpeed + 1.0) / 1.0f);
			if(worldGame.speed > 5)
				worldGame.speed = 5;
		} else {
			worldGame.timeSpeed = 0;
			worldGame.speed = Constant.SPEED;
		}
		
		if (worldGame.score != lastScore) {
			lastScore = worldGame.score;
		}
		
		// If Level End
		if (worldGame.state == WorldGame.STATE.WORLD_NEXT_LEVEL) {
			state = STATE.GAME_LEVEL_END;
			
			// Next Level
			
			System.gc();
			worldGame.dispose();
		}

		// If Game Over
		if (worldGame.state == WorldGame.STATE.WORLD_GAME_OVER) {
			state = STATE.GAME_OVER;
			
			// End Game
			Setting.score = lastScore;
			System.gc();
			worldGame.dispose();
		}

	}

	// Game Pause
	private void updatePause(float deltaTime) {
		// TODO Auto-generated method stub
		for (Body body : worldGame.bodies) {
			body.setLinearVelocity(0, 0);
		}
	}

	// Game Level End
	private void updateLevelEnd(float deltaTime) {
		// TODO Auto-generated method stub
		
		// Win, next level
		
	}

	// Game Over
	private void updateGameOver(float deltaTime) {
		// TODO Auto-generated method stub
		Assets.dichuyen.pause();
		Assets.tangtoc.pause();
		worldGame.speed = Constant.SPEED;
		worldGame.timeOver += deltaTime;
		if(worldGame.timeOver < 2.5f) {
			if(worldGame.motorbike.getBody().getPosition().x > 0)
				worldGame.motorbike.getBody().setAngularVelocity(90 * Constant.DEGTORAD);
			else
				worldGame.motorbike.getBody().setAngularVelocity(-90 * Constant.DEGTORAD);
			Vector2 vel = worldGame.motorbike.getBody().getLinearVelocity();
			vel.y = 0;
			worldGame.motorbike.getBody().setLinearVelocity(vel);
		} else {
			worldGame.motorbike.getBody().setAngularVelocity(0);
			worldGame.motorbike.getBody().setLinearVelocity(0, 0);
		}
		System.gc();
		// Game over
		if(Gdx.input.justTouched()) {
			MotorBike.getManagerScreen().createScreen(ManagerScreen.SCREEN_LOSE);
		}
	}
	
	// Draw Objects
	private void draw(float deltaTime) {
		// TODO Auto-generated method stub
		batch.begin();
		float width = camera.viewportWidth;
		float height = camera.viewportHeight;
		
		// map 1
		if(worldGame.map == 1) {
			batch.draw(Assets.map_1, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
			
			// Tree
			float tree_width = Assets.tree.getRegionWidth() * worldGame.sizeX;
			float tree_height = Assets.tree.getRegionHeight() * worldGame.sizeY;
			
			batch.draw(Assets.tree, (90 * worldGame.sizeX - worldGame.x_tree), height * 1.5f - worldGame.y_tree, tree_width, tree_height);
			batch.draw(Assets.tree, (10 * worldGame.sizeX - worldGame.x_tree), height - worldGame.y_tree, tree_width, tree_height);
			batch.draw(Assets.tree, (-tree_width / 2 - worldGame.x_tree), height / 2 - worldGame.y_tree, tree_width, tree_height);
			
			batch.draw(Assets.tree, width - tree_width - (90 * worldGame.sizeX - worldGame.x_tree), height * 1.5f - worldGame.y_tree, tree_width, tree_height);
			batch.draw(Assets.tree, width - tree_width - (10 * worldGame.sizeX - worldGame.x_tree), height - worldGame.y_tree, tree_width, tree_height);
			batch.draw(Assets.tree, width - tree_width - (-tree_width / 2 - worldGame.x_tree), height / 2 - worldGame.y_tree, tree_width, tree_height);
			
		} 
		// map 2
		else if(worldGame.map == 2) {
			batch.draw(Assets.map_2, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
			
			// Den ham
			float den_width = Assets.den_ham.getRegionWidth() * worldGame.sizeX;
			float den_height = Assets.den_ham.getRegionHeight() * worldGame.sizeY;
			
			batch.draw(Assets.den_ham, (210 * worldGame.sizeX - worldGame.x_den_ham), height * 1.5f - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, (150 * worldGame.sizeX - worldGame.x_den_ham), height * 1.25f - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, (90 * worldGame.sizeX - worldGame.x_den_ham), height - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, (30 * worldGame.sizeX - worldGame.x_den_ham), height * 0.75f - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, (-den_width / 2 - worldGame.x_den_ham), height / 2 - worldGame.y_den_ham, den_width, den_height);
			
			batch.draw(Assets.den_ham, width - den_width - (210 * worldGame.sizeX - worldGame.x_den_ham), height * 1.5f - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, width - den_width - (150 * worldGame.sizeX - worldGame.x_den_ham), height * 1.25f - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, width - den_width - (90 * worldGame.sizeX - worldGame.x_den_ham), height - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, width - den_width - (30 * worldGame.sizeX - worldGame.x_den_ham), height * 0.75f - worldGame.y_den_ham, den_width, den_height);
			batch.draw(Assets.den_ham, width - den_width - (-den_width / 2 - worldGame.x_den_ham), height / 2 - worldGame.y_den_ham, den_width, den_height);
			
			batch.draw(Assets.den_ham, (10 * worldGame.sizeX), height * 0.95f, den_width, den_height);
			batch.draw(Assets.den_ham, (-35 * worldGame.sizeX), height * 0.87f, den_width, den_height);
			batch.draw(Assets.den_ham, width - den_width - (5 * worldGame.sizeX), height * 0.95f, den_width, den_height);
			batch.draw(Assets.den_ham, width - den_width - (-35 * worldGame.sizeX), height * 0.87f, den_width, den_height);
			
		} 
		// map 3
		else {
			batch.draw(Assets.map_3, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
			
			// Den duong
			float den_width = Assets.den_duong.getRegionWidth() * worldGame.sizeX;
			float den_height = Assets.den_duong.getRegionHeight() * worldGame.sizeY;
			
			batch.draw(Assets.den_duong, (210 * worldGame.sizeX - worldGame.x_den_duong), height * 1.5f - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong, (150 * worldGame.sizeX - worldGame.x_den_duong), height * 1.25f - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong, (90 * worldGame.sizeX - worldGame.x_den_duong), height - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong, (30 * worldGame.sizeX - worldGame.x_den_duong), height * 0.75f - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong, (-den_width / 2 - worldGame.x_den_duong), height / 2 - worldGame.y_den_duong, den_width, den_height);
			
			batch.draw(Assets.den_duong_flip, width - den_width - (210 * worldGame.sizeX - worldGame.x_den_duong), height * 1.5f - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong_flip, width - den_width - (150 * worldGame.sizeX - worldGame.x_den_duong), height * 1.25f - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong_flip, width - den_width - (90 * worldGame.sizeX - worldGame.x_den_duong), height - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong_flip, width - den_width - (30 * worldGame.sizeX - worldGame.x_den_duong), height * 0.75f - worldGame.y_den_duong, den_width, den_height);
			batch.draw(Assets.den_duong_flip, width - den_width - (-den_width / 2 - worldGame.x_den_duong), height / 2 - worldGame.y_den_duong, den_width, den_height);
			
		}
		
		batch.draw(Assets.buttonSpeed, camera.viewportWidth / 2 - Assets.buttonSpeed.getRegionWidth() * worldGame.sizeX / 2, 0, Assets.buttonSpeed.getRegionWidth() * worldGame.sizeX, Assets.buttonSpeed.getRegionHeight() * worldGame.sizeY);
		if(worldGame.speed <= 3)
			batch.draw(Assets.kim_dong_ho, camera.viewportWidth / 2 - Assets.kim_dong_ho.getRegionWidth() * worldGame.sizeX / 2, 25 * worldGame.sizeY, 0, 0, Assets.kim_dong_ho.getRegionWidth() * worldGame.sizeX, Assets.kim_dong_ho.getRegionHeight() * worldGame.sizeY, 1, 1, 45.0f * (3.0f - worldGame.speed));
		else
			batch.draw(Assets.kim_dong_ho, camera.viewportWidth / 2 - Assets.kim_dong_ho.getRegionWidth() * worldGame.sizeX / 2, 25 * worldGame.sizeY, 0, 0, Assets.kim_dong_ho.getRegionWidth() * worldGame.sizeX, Assets.kim_dong_ho.getRegionHeight() * worldGame.sizeY, 1, 1, -45.0f * (worldGame.speed - 3.0f));
	
		Assets.font_2.setScale(worldGame.sizeX, worldGame.sizeY);
		Assets.font_2.draw(batch, ""+lastScore, 150 * worldGame.sizeX, camera.viewportHeight - 130 * worldGame.sizeY);
		Assets.font_2.draw(batch, ""+(int)worldGame.speed, 430 * worldGame.sizeX, camera.viewportHeight - 130 * worldGame.sizeY);
		batch.draw(Assets.scoreImage, 10 * worldGame.sizeX,  camera.viewportHeight - 162 * worldGame.sizeY, Assets.scoreImage.getRegionWidth() * worldGame.sizeX, Assets.scoreImage.getRegionHeight() * worldGame.sizeY);
		batch.draw(Assets.speedImage, 300 * worldGame.sizeX,  camera.viewportHeight - 160 * worldGame.sizeY, Assets.speedImage.getRegionWidth() * worldGame.sizeX, Assets.speedImage.getRegionHeight() * worldGame.sizeY);
		
		batch.end();
	}

	// Draw Sprite
	private void drawSpriteToBody() {
		batch.setProjectionMatrix(camera2.combined);
		batch.begin();
		worldGame.worldBox2D.getBodies(worldGame.bodies);
		for (Body body : worldGame.bodies) {
			if(body.getUserData() != null && body.getUserData() instanceof UserData && body.isActive()) {
				UserData data = (UserData) body.getUserData();
				Sprite sprite = (Sprite)data.getSprite();
				if(sprite != null) {
					sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
					sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
					sprite.draw(batch);
				}
			}
		}
		batch.end();
		
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		for (Car car : worldGame.activeCars) {
			if(car.direction == 1)
				batch.draw(Assets.xi_nhan, (car.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER  - car.getWidth() / 1.5f, (car.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - car.getHeight(), Assets.xi_nhan.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan.getRegionHeight() * worldGame.sizeY);
			else if(car.direction == 2)
				batch.draw(Assets.xi_nhan, (car.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER, (car.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - car.getHeight(), Assets.xi_nhan.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan.getRegionHeight() * worldGame.sizeY);
			else if(car.direction == 0){
				batch.draw(Assets.xi_nhan, (car.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER  - car.getWidth() / 1.5f, (car.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - car.getHeight(), Assets.xi_nhan.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan.getRegionHeight() * worldGame.sizeY);
				batch.draw(Assets.xi_nhan, (car.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER, (car.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - car.getHeight(), Assets.xi_nhan.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan.getRegionHeight() * worldGame.sizeY);
			}
			//batch.draw(Assets.den_oto, (car.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER - car.getWidth() / 1.5f, (car.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER + car.getHeight() / 2);
		}
		if(worldGame.motorbike.getState() != Object.STATE.DIE) {
			if(worldGame.speed > 1) {
				if(worldGame.speed <= 3) {
					batch.draw(Assets.den_oto, (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getWidth() / 1.6f, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER, Assets.den_oto_2.getRegionWidth() * worldGame.sizeX, Assets.den_oto_2.getRegionHeight() * worldGame.sizeY);
					batch.draw(Assets.xi_nhan, (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getWidth() / 1.5f, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getHeight(), Assets.xi_nhan.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan.getRegionHeight() * worldGame.sizeY);
					batch.draw(Assets.xi_nhan, (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getHeight(), Assets.xi_nhan.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan.getRegionHeight() * worldGame.sizeY);
				} else {
					batch.draw(Assets.den_oto_2, (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getWidth() / 1.6f, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER + 5 * worldGame.sizeY, Assets.den_oto_2.getRegionWidth() * worldGame.sizeX, Assets.den_oto_2.getRegionHeight() * worldGame.sizeY);
					batch.draw(Assets.xi_nhan_2, (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getWidth() / 1.5f, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getHeight(), Assets.xi_nhan_2.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan_2.getRegionHeight() * worldGame.sizeY);
					batch.draw(Assets.xi_nhan_2, (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getHeight(), Assets.xi_nhan_2.getRegionWidth() * worldGame.sizeX, Assets.xi_nhan_2.getRegionHeight() * worldGame.sizeY);
				}
			}
		}
		
		Assets.font_2.setScale(worldGame.sizeX, worldGame.sizeY);
		if(state == STATE.GAME_PAUSE) {	
			if (isQuit) {
				batch.draw(Assets.checkQuit, 20 * worldGame.sizeX, 400 * worldGame.sizeY, Assets.checkQuit.getRegionWidth() * worldGame.sizeX, Assets.checkQuit.getRegionHeight() * worldGame.sizeY);
				batch.draw(Assets.yes, 50 * worldGame.sizeX, 320 * worldGame.sizeY, Assets.yes.getRegionWidth() * worldGame.sizeX, Assets.yes.getRegionHeight() * worldGame.sizeY);
				batch.draw(Assets.no, 280 * worldGame.sizeX, 320 * worldGame.sizeY, Assets.no.getRegionWidth() * worldGame.sizeX, Assets.no.getRegionHeight() * worldGame.sizeY);
				//Assets.font_2.draw(batch, "Are you sure to quit?", 120 * worldGame.sizeX, 400 * worldGame.sizeY);
				//Assets.font_2.draw(batch, "Yes", 180 * worldGame.sizeX, 350 * worldGame.sizeY);
				//Assets.font_2.draw(batch, "No", 280 * worldGame.sizeX, 350 * worldGame.sizeY);
			} else {
				if(isResumeClick)
					batch.draw(Assets.resume_click, 0, 10 * worldGame.sizeX, Assets.resume.getRegionWidth() * worldGame.sizeX, Assets.resume.getRegionHeight() * worldGame.sizeY);
				else
					batch.draw(Assets.resume, 0, 10 * worldGame.sizeX, Assets.resume.getRegionWidth() * worldGame.sizeX, Assets.resume.getRegionHeight() * worldGame.sizeY);
				if(isQuitClick)
					batch.draw(Assets.quit_click, Gdx.graphics.getWidth() - Assets.quit.getRegionWidth() * worldGame.sizeX, 10 * worldGame.sizeX, Assets.quit.getRegionWidth() * worldGame.sizeX, Assets.quit.getRegionHeight() * worldGame.sizeY);
				else
					batch.draw(Assets.quit, Gdx.graphics.getWidth() - Assets.quit.getRegionWidth() * worldGame.sizeX, 10 * worldGame.sizeX, Assets.quit.getRegionWidth() * worldGame.sizeX, Assets.quit.getRegionHeight() * worldGame.sizeY);
				/*Assets.font_2.draw(batch, "RESUME", 50 * worldGame.sizeX, 100 * worldGame.sizeY);
				Assets.font_2.draw(batch, "GAME", 50 * worldGame.sizeX, 50 * worldGame.sizeY);
				Assets.font_2.draw(batch, "QUIT", 350 * worldGame.sizeX, 100 * worldGame.sizeY);
				Assets.font_2.draw(batch, "GAME", 350 * worldGame.sizeX, 50 * worldGame.sizeY);*/
			}
			
		} else if(state == STATE.GAME_RUNNING){
			//if (isQuit) {
				//Assets.font_2.draw(batch, "Are you sure to quit?", 120 * worldGame.sizeX, 400 * worldGame.sizeY);
				//Assets.font_2.draw(batch, "Yes", 180 * worldGame.sizeX, 350 * worldGame.sizeY);
				//Assets.font_2.draw(batch, "No", 280 * worldGame.sizeX, 350 * worldGame.sizeY);
			//} else {
				if(isPauseClick)
					batch.draw(Assets.pause_click, 0, 10 * worldGame.sizeX, Assets.pause.getRegionWidth() * worldGame.sizeX, Assets.pause.getRegionHeight() * worldGame.sizeY);
				else
					batch.draw(Assets.pause, 0, 10 * worldGame.sizeX, Assets.pause.getRegionWidth() * worldGame.sizeX, Assets.pause.getRegionHeight() * worldGame.sizeY);
				//Assets.font_2.draw(batch, "PAUSE", 50 * worldGame.sizeX, 100 * worldGame.sizeY);
				//Assets.font_2.draw(batch, "GAME", 50 * worldGame.sizeX, 50 * worldGame.sizeY);
			//}
		}
		
		if(state == STATE.GAME_OVER)
			batch.draw(Assets.exploreAnimation.getKeyFrame(worldGame.timeOver, false), (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER  - worldGame.motorbike.getWidth() / 1.5f, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getHeight() / 2, Assets.explore[0].getRegionWidth() * worldGame.sizeX, Assets.explore[0].getRegionHeight() * worldGame.sizeY);
		
		switch (state) {
		case GAME_READY:
			presentReady();
			break;
		case GAME_RUNNING:
			presentRunning();
			break;
		case GAME_PAUSE:
			presentPaused();
			break;
		case GAME_LEVEL_END:
			presentLevelEnd();
			break;
		case GAME_OVER:
			presentGameOver();
			break;
		default:
			break;
		}
		
		batch.end();
	}
	
	// Draw Game Ready
	private void presentReady() {
		// TODO Auto-generated method stub
		batch.draw(Assets.readyImage, 80 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 25 * worldGame.sizeY, Assets.readyImage.getRegionWidth() * worldGame.sizeX, Assets.readyImage.getRegionHeight() * worldGame.sizeY);
		//Assets.font_2.draw(batch, "READY", Gdx.graphics.getWidth() / 2 - 40 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 25 * worldGame.sizeY);
	}

	// Draw Game Running
	private void presentRunning() {
		// TODO Auto-generated method stub
		//Assets.font.draw(batch, "RUNNING", Gdx.graphics.getWidth() / 2 - 50 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 75 * worldGame.sizeY);
		// Used memory
		/*long freeMemory = Runtime.getRuntime().maxMemory()
				- Runtime.getRuntime().freeMemory();
		Assets.font.draw(batch, String.valueOf(freeMemory), Gdx.graphics.getWidth() / 2 - 50 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 25 * worldGame.sizeY);*/
	}

	// Draw Game Pause
	private void presentPaused() {
		// TODO Auto-generated method stub
		if(!isQuit)
			batch.draw(Assets.pauseImage, 125 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 25 * worldGame.sizeY, Assets.pauseImage.getRegionWidth() * worldGame.sizeX, Assets.pauseImage.getRegionHeight() * worldGame.sizeY);
			//Assets.font_2.draw(batch, "PAUSE", Gdx.graphics.getWidth() / 2 - 40 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 100 * worldGame.sizeY);
	}

	// Draw Game Level End
	private void presentLevelEnd() {
		// TODO Auto-generated method stub
		Assets.font_2.draw(batch, "LEVEL COMPLETED...", Gdx.graphics.getWidth() / 2 - 100 * worldGame.sizeX,
				Gdx.graphics.getHeight() / 2 + 25 * worldGame.sizeY);
	}

	// Draw Game Over
	private void presentGameOver() {
		// TODO Auto-generated method stub
		//batch.draw(Assets.exploreAnimation.getKeyFrame(worldGame.timeOver, false), (worldGame.motorbike.getBody().getPosition().x + worldGame.WIDTH / 2) * Constant.PIXEL_PER_METER  - worldGame.motorbike.getWidth() / 2, (worldGame.motorbike.getBody().getPosition().y + worldGame.HEIGHT / 2) * Constant.PIXEL_PER_METER - worldGame.motorbike.getHeight() / 2);
		batch.draw(Assets.gameoverImage, 30 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 25 * worldGame.sizeY, Assets.gameoverImage.getRegionWidth() * worldGame.sizeX, Assets.gameoverImage.getRegionHeight() * worldGame.sizeY);
		//Assets.font_2.draw(batch, "GAME OVER", Gdx.graphics.getWidth() / 2 - 70 * worldGame.sizeX, Gdx.graphics.getHeight() / 2 + 25 * worldGame.sizeY);
	}
	
	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		camera2.viewportWidth = width / Constant.PIXEL_PER_METER;
		camera2.viewportHeight = height / Constant.PIXEL_PER_METER;
		//camera2.update();
		
		batch.setProjectionMatrix(camera2.combined);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		super.pause();
		if(state == STATE.GAME_RUNNING)
			state = STATE.GAME_PAUSE;
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		super.resume();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
		worldGame.dispose();
		//Assets.dichuyen.dispose();
		//Assets.tangtoc.dispose();
		//Assets.vacham.dispose();
		
	}

}
